#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
修复数据库中错误的图片路径
"""

import os
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TradingPlatform_Server.settings')
django.setup()

from Server.models import Takeout, User

def fix_takeout_images():
    """修复外卖图片路径"""
    print("开始修复外卖图片路径...")
    
    # 获取所有外卖记录
    takeouts = Takeout.objects.all()
    
    # 外卖图片映射（示例）
    takeout_images = {
        '宫保鸡丁': 'takeouts/default/鸡腿.png',
        '麻婆豆腐': 'takeouts/default/米饭.png',
        '糖醋里脊': 'takeouts/default/炒饭.png',
        '红烧肉': 'takeouts/default/米饭.png',
        '鱼香肉丝': 'takeouts/default/炒饭.png',
        '回锅肉': 'takeouts/default/米饭.png',
        '水煮鱼': 'takeouts/default/火锅.png',
        '汉堡': 'takeouts/default/汉堡.png',
        '披萨': 'takeouts/default/披萨.png',
        '三明治': 'takeouts/default/三明治.png',
        '寿司': 'takeouts/default/寿司.png',
        '拉面': 'takeouts/default/面条.png',
        '炒饭': 'takeouts/default/炒饭.png',
        '煎饺': 'takeouts/default/饺子.png',
        '小笼包': 'takeouts/default/包子.png',
        '火锅': 'takeouts/default/火锅.png',
        '奶茶': 'takeouts/default/奶茶.png',
        '咖啡': 'takeouts/default/咖啡.png',
        '果汁': 'takeouts/default/果汁.png',
        '冰淇淋': 'takeouts/default/冰淇淋.png',
        '蛋糕': 'takeouts/default/甜点.png',
        '沙拉': 'takeouts/default/沙拉.png',
        '薯条': 'takeouts/default/薯条.png',
    }
    
    updated_count = 0
    for takeout in takeouts:
        # 如果图片路径包含 user_portrait 或为空，则需要修复
        if not takeout.takeout_portrait or 'user_portrait' in str(takeout.takeout_portrait):
            # 根据外卖名称匹配图片
            new_image = None
            for keyword, image_path in takeout_images.items():
                if keyword in takeout.takeout_name:
                    new_image = image_path
                    break
            
            # 如果没有匹配到，使用默认图片
            if not new_image:
                new_image = 'takeouts/default/takeout_portrait.png'
            
            takeout.takeout_portrait = new_image
            takeout.save()
            updated_count += 1
            print(f"✓ 更新外卖: {takeout.takeout_name} -> {new_image}")
    
    print(f"\n完成！共更新 {updated_count} 条外卖记录")


def fix_user_images():
    """修复用户头像路径"""
    print("\n开始检查用户头像路径...")
    
    users = User.objects.all()
    updated_count = 0
    
    for user in users:
        # 如果头像路径包含 takeouts，则需要修复
        if user.head_portrait and 'takeouts' in str(user.head_portrait):
            user.head_portrait = 'users/default/user_portrait.png'
            user.save()
            updated_count += 1
            print(f"✓ 更新用户: {user.username} -> users/default/user_portrait.png")
    
    if updated_count == 0:
        print("○ 所有用户头像路径正确")
    else:
        print(f"\n完成！共更新 {updated_count} 条用户记录")


def main():
    print("=" * 60)
    print("数据库图片路径修复")
    print("=" * 60)
    
    # 修复外卖图片
    fix_takeout_images()
    
    # 修复用户头像
    fix_user_images()
    
    print("\n" + "=" * 60)
    print("修复完成！")
    print("=" * 60)


if __name__ == '__main__':
    main()

